*******************************************************************************
*         CONVERT RAW HCUP DATA INTO VARIOUS DATASETS FOR ANALYSIS.           *
*******************************************************************************

log using "${SIDCodePath}/sidsedd_merge.log", replace
*-------------------------------------------------- 
* Create yearly SID, SEDD datasets of core variables
*--------------------------------------------------
forval i = 2010/2016 {
 	
	if `i' < 2015 {
		// Inpatient
			use "${SIDPath}/`i'/fl_sid_`i'_core.dta", clear
			rename  *, lower
		    capture confirm var dx_admitting
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen dx_admitting = ""
		    }
		    capture confirm var dx_visit_reason1
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen     dx_visit_reason1 = ""
		    }
			keep    ahour edhour pay1 pay1_x hcup_ed year age los totchg hcup_os dqtr dhour los_x drg dx* /// 
				    female visitlink daystoevent dshospid aweekend key  dx_admitting ndx mdnum* pointoforigin_x ///
				    race_x hispanic_x nchronic medincstq orproc zip died npr dx_visit_reason* /* CHANGE MEANS HERE */
			gen     inpatient = 1
			gen     eweekend = .
			save    "${SIDDataPath}/fl_sid_`i'_core_short.dta", replace

		// Emergency department
			use "${SEDDPath}/`i'/fl_sedd_`i'_core.dta", clear
			rename  *, lower

		    capture confirm var dx_admitting
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen dx_admitting = ""
		    }
			keep    ahour pay1 pay1_x hcup_ed year age los totchg hcup_os dhour los_x dqtr dx* ///
					female visitlink daystoevent dshospid aweekend key dx_visit_reason  dx_admitting ndx ///
					mdnum* pointoforigin_x race_x hispanic_x nchronic medincstq orproc zip died npr /* CHANGE MEANS HERE */
			gen 	inpatient = 0
			gen 	drg = .
			rename 	ahour edhour /* what is considered "admission hour" in the ED is edhour in the inpatient data. patients in the ED data have no ahour since they were *not* admitted */
			rename 	aweekend eweekend 
			save "${SIDDataPath}/fl_sedd_`i'_core_short.dta", replace
	} 


	if `i' == 2015{
		 // Inpatient
			use "${SIDPath}/`i'/fl_sid_`i'q1q3_core.dta", clear
			rename  *, lower
			keep 	ahour edhour pay1 pay1_x hcup_ed year age los totchg hcup_os dqtr dhour los_x drg dx* /// 
					female visitlink daystoevent dshospid aweekend key  dx_admitting ndx mdnum* pointoforigin_x ///
					race_x hispanic_x nchronic medincstq orproc zip died npr /* CHANGE MEANS HERE */
			gen 	inpatient = 1
			gen 	eweekend = .
			gen 	dx_visit_reason1 = ""
			save "${SIDDataPath}/fl_sid_`i'q1q3_core_short.dta", replace

			use "${SIDPath}/`i'/fl_sid_`i'q4_core.dta", clear
			capture confirm var i10_dx_admitting
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen dx_admitting = ""
		    }

			rename 	i10_dx* dx* /* must rename i10_dx* to dx* because the diagnosis code changed from i9 to i10 in the 4th quarter of 2015 */
			rename 	i10_orproc orproc
			rename 	i10_nchronic nchronic
			rename 	i10_ndx ndx 
			rename 	i10_npr npr

			keep 	ahour edhour pay1 pay1_x hcup_ed year age los totchg hcup_os dqtr dhour los_x drg dx* female visitlink ///
					daystoevent dshospid aweekend key  ndx dx_admitting mdnum* pointoforigin_x race_x hispanic_x  /// 
					nchronic medincstq orproc zip died npr /* CHANGE MEANS HERE */
			gen 	inpatient = 1
			gen 	eweekend = .
			gen 	dx_visit_reason1 = ""

			append 	using "${SIDDataPath}/fl_sid_`i'q1q3_core_short.dta"
			save "${SIDDataPath}/fl_sid_`i'_core_short.dta", replace

		// Emergency department
			use "${SEDDPath}/`i'/fl_sedd_`i'q1q3_core.dta", clear
			rename  *, lower
			capture confirm var dx_admitting
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen dx_admitting = ""
		    }

			rename  *, lower
			keep 	ahour pay1 pay1_x hcup_ed year age los totchg hcup_os dhour los_x dqtr dx* female visitlink daystoevent dx_admitting ///
					dshospid aweekend key dx_visit_reason*  ndx mdnum* pointoforigin_x race_x hispanic_x nchronic /// 
					medincstq orproc zip died npr /* CHANGE MEANS HERE */
			rename 	ahour edhour
			gen 	inpatient = 0
			gen 	drg = .
			rename 	aweekend eweekend
			save "${SIDDataPath}/fl_sedd_`i'q1q3_core_short.dta", replace
			 

			use "${SEDDPath}/`i'/fl_sedd_`i'q4_core.dta", clear
			capture confirm var i10_dx_admitting
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen dx_admitting = ""
		    }
			rename 	*, lower
			rename 	i10_dx* dx*
			rename 	i10_nchronic nchronic
			rename 	i10_ndx ndx 
			rename 	i10_orproc orproc
			rename 	i10_npr npr

			keep 	ahour pay1 pay1_x hcup_ed year age los totchg hcup_os dhour los_x dqtr dx* female visitlink daystoevent ///
					dshospid aweekend key dx_visit_reason*  ndx mdnum* pointoforigin_x race_x hispanic_x nchronic dx_visit_reason ///
					medincstq orproc zip died npr /* CHANGE MEANS HERE */
			rename 	ahour edhour
			gen 	inpatient = 0
			gen 	drg = .

			rename 	aweekend eweekend
			append 	using "${SIDDataPath}/fl_sedd_`i'q1q3_core_short.dta"
			save "${SIDDataPath}/fl_sedd_`i'_core_short.dta", replace 

	} 

	if `i' == 2016{
		// Inpatient
			use "${SIDPath}/`i'/fl_sid_`i'_core.dta", clear
			rename  *, lower
			capture confirm var i10_dx_admitting
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen dx_admitting = ""
		    }

			rename 	i10_dx* dx*
			rename 	i10_ndx ndx 
			rename 	i10_npr npr

			keep 	ahour edhour pay1 pay1_x hcup_ed year age los totchg hcup_os dqtr dhour los_x drg dx* female visitlink ///
					daystoevent dshospid aweekend key  dx_admitting ndx mdnum* pointoforigin_x race_x hispanic_x /// 
					medincstq zip died npr /* CHANGE MEANS HERE */
			gen 	inpatient = 1
			gen 	eweekend = .
			gen 	dx_visit_reason1 = ""
			gen 	nchronic = .
			gen 	orproc = .
			save "${SIDDataPath}/fl_sid_`i'_core_short.dta", replace

		// Emergency department
			use "${SEDDPath}/`i'/fl_sedd_`i'_core.dta", clear
			rename  *, lower
			capture confirm var i10_dx_admitting
		    if c(rc) == 111 { // VARIABLE NOT FOUND
		        gen dx_admitting = ""
		    }

			rename 	*, lower
			rename 	i10_dx* dx*
			rename 	i10_ndx ndx 
			rename 	i10_npr npr
			keep 	ahour pay1 pay1_x hcup_ed year age los totchg hcup_os dhour los_x dqtr dx* female visitlink daystoevent dx_admitting /// 
					dshospid aweekend key dx_visit_reason*  ndx mdnum* pointoforigin_x race_x hispanic_x medincstq zip died npr /* CHANGE MEANS HERE */
			gen 	inpatient = 0
			gen 	drg = .
			gen 	orproc = .
			gen 	nchronic = .
			rename 	aweekend eweekend 
			rename 	ahour edhour /* what is considered "admission hour" in the ED is edhour in the inpatient data. patients in the ED data have no ahour since they were *not* admitted */
			save "${SIDDataPath}/fl_sedd_`i'_core_short.dta", replace
	}
}  

*-------------------------------------------------- 
* Create SID, SEDD datasets of charges from 2010-2016
*--------------------------------------------------
capture mkdir "${SIDDataPath}/charges/"
use "${SIDPath}/2010/fl_sid_2010_chgs.dta", clear 
keep key chg10 chg10 chg11 chg19 chg23 chg18
rename chg10 chg_lab
rename chg11 chg_rad
rename chg19 chg_car
rename chg23 chg_obs
rename chg18 chg_ED

gen chg_lab_d = cond(chg_lab > 0, 1, 0)
gen chg_rad_d = cond(chg_rad > 0, 1, 0)
gen chg_car_d = cond(chg_car > 0, 1, 0)
gen chg_obs_d = cond(chg_obs > 0, 1, 0)
gen chg_ED_d  = cond(chg_ED > 0, 1, 0)

save "${SIDDataPath}/charges/fl_sid_chgs.dta", replace

// Inpatient
	// Chg10: laboratory charges
	// Chg11: radiology or other imaging charges
	// Chg19: cardiology charges (i.e., cath lab)
	// Chg23: treatment or observation room charges 
	// Chg18: emergency department charges
	forval i = 2011/2016 {
		di `i'
		//merge key using "${SIDDataPath}/fl_sedd_`i'_core_short.dta", keep(1 3) keepusing
		if `i' != 2015{
			use "${SIDPath}/`i'/fl_sid_`i'_chgs.dta", clear 
			rename *, lower
			keep   key chg10 chg10 chg11 chg19 chg23 chg18
			rename chg10 chg_lab
			rename chg11 chg_rad
			rename chg19 chg_car
			rename chg23 chg_obs
			rename chg18 chg_ED

			gen chg_lab_d = cond(chg_lab > 0, 1, 0)
			gen chg_rad_d = cond(chg_rad > 0, 1, 0)
			gen chg_car_d = cond(chg_car > 0, 1, 0)
			gen chg_obs_d = cond(chg_obs > 0, 1, 0)
			gen chg_ED_d  = cond(chg_ED > 0, 1, 0)
			append using "${SIDDataPath}/charges/fl_sid_chgs.dta"
			save "${SIDDataPath}/charges/fl_sid_chgs.dta", replace
		}
		if `i' == 2015{
			use "${SIDPath}/`i'/fl_sid_`i'q1q3_chgs.dta", clear 
			rename *, lower
			keep   key chg10 chg10 chg11 chg19 chg23 chg18
			rename chg10 chg_lab
			rename chg11 chg_rad
			rename chg19 chg_car
			rename chg23 chg_obs
			rename chg18 chg_ED

			gen chg_lab_d = cond(chg_lab > 0, 1, 0)
			gen chg_rad_d = cond(chg_rad > 0, 1, 0)
			gen chg_car_d = cond(chg_car > 0, 1, 0)
			gen chg_obs_d = cond(chg_obs > 0, 1, 0)
			gen chg_ED_d  = cond(chg_ED > 0, 1, 0)
			append using "${SIDDataPath}/charges/fl_sid_chgs.dta"
			save "${SIDDataPath}/charges/fl_sid_chgs.dta", replace

			use "${SIDPath}/`i'/fl_sid_`i'q4_chgs.dta", clear 
			rename *, lower
			keep   key chg10 chg10 chg11 chg19 chg23 chg18
			rename chg10 chg_lab
			rename chg11 chg_rad
			rename chg19 chg_car
			rename chg23 chg_obs
			rename chg18 chg_ED

			gen chg_lab_d = cond(chg_lab > 0, 1, 0)
			gen chg_rad_d = cond(chg_rad > 0, 1, 0)
			gen chg_car_d = cond(chg_car > 0, 1, 0)
			gen chg_obs_d = cond(chg_obs > 0, 1, 0)
			gen chg_ED_d  = cond(chg_ED > 0, 1, 0)
			append using "${SIDDataPath}/charges/fl_sid_chgs.dta"
			save "${SIDDataPath}/charges/fl_sid_chgs.dta", replace
		}
	}
	// each key should be unique
	use "${SIDDataPath}/charges/fl_sid_chgs.dta", clear
	duplicates drop key, force
	save "${SIDDataPath}/charges/fl_sid_chgs.dta", replace

	// ED
		// Chg3: laboratory charges
		// Chg4: radiology and other imaging
		// Chg5: cardiology
		// Chg11: treatment or observation room charges
		// Chg18: emergency department charges
		use "${SEDDPath}/2010/fl_sedd_2010_chgs.dta", clear 
		rename *, lower

		keep key chg3 chg4 chg5 chg11 chg9
		rename chg3 chg_lab
		rename chg4 chg_rad
		rename chg5 chg_car
		rename chg11 chg_obs
		rename chg9 chg_ED

		gen chg_lab_d = cond(chg_lab > 0, 1, 0)
		gen chg_rad_d = cond(chg_rad > 0, 1, 0)
		gen chg_car_d = cond(chg_car > 0, 1, 0)
		gen chg_obs_d = cond(chg_obs > 0, 1, 0)
		gen chg_ED_d = cond(chg_ED > 0, 1, 0)

		save "${SIDDataPath}/charges/fl_sedd_chgs.dta", replace

		forval i = 2011/2016 {
			if `i' != 2015{
				di `i'
				use "${SEDDPath}/`i'/fl_sedd_`i'_chgs.dta", clear 
				rename *, lower
				keep   key chg3 chg4 chg5 chg11 chg9
				rename chg3  chg_lab
				rename chg4  chg_rad
				rename chg5  chg_car
				rename chg11 chg_obs
				rename chg9 chg_ED

				gen chg_lab_d = cond(chg_lab > 0, 1, 0)
				gen chg_rad_d = cond(chg_rad > 0, 1, 0)
				gen chg_car_d = cond(chg_car > 0, 1, 0)
				gen chg_obs_d = cond(chg_obs > 0, 1, 0)
				gen chg_ED_d = cond(chg_ED > 0, 1, 0)

				append using "${SIDDataPath}/charges/fl_sedd_chgs.dta"
				save "${SIDDataPath}/charges/fl_sedd_chgs.dta", replace
			}
			if `i' == 2015{
				use "${SEDDPath}/`i'/fl_sedd_`i'q1q3_chgs.dta", clear 
				rename *, lower
				keep   key chg3 chg4 chg5 chg11 chg9
				rename chg3  chg_lab
				rename chg4  chg_rad
				rename chg5  chg_car
				rename chg11 chg_obs
				rename chg9 chg_ED

				gen chg_lab_d = cond(chg_lab > 0, 1, 0)
				gen chg_rad_d = cond(chg_rad > 0, 1, 0)
				gen chg_car_d = cond(chg_car > 0, 1, 0)
				gen chg_obs_d = cond(chg_obs > 0, 1, 0)
				gen chg_ED_d = cond(chg_ED > 0, 1, 0)

				append using "${SIDDataPath}/charges/fl_sedd_chgs.dta"
				save "${SIDDataPath}/charges/fl_sedd_chgs.dta", replace

				use "${SEDDPath}/`i'/fl_sedd_`i'q4_chgs.dta", clear 
				rename *, lower
				keep   key chg3 chg4 chg5 chg11 chg9
				rename chg3  chg_lab
				rename chg4  chg_rad
				rename chg5  chg_car
				rename chg11 chg_obs
				rename chg9 chg_ED

				gen chg_lab_d = cond(chg_lab > 0, 1, 0)
				gen chg_rad_d = cond(chg_rad > 0, 1, 0)
				gen chg_car_d = cond(chg_car > 0, 1, 0)
				gen chg_obs_d = cond(chg_obs > 0, 1, 0)
				gen chg_ED_d = cond(chg_ED > 0, 1, 0)

				append using "${SIDDataPath}/charges/fl_sedd_chgs.dta"
				save "${SIDDataPath}/charges/fl_sedd_chgs.dta", replace
			}
		}

	use "${SIDDataPath}/charges/fl_sedd_chgs.dta", clear
	duplicates drop key, force
	save "${SIDDataPath}/charges/fl_sedd_chgs.dta", replace

*-------------------------------------------------- 
* Combine core 2010-2016 SID, SEDD datasets together
*--------------------------------------------------
	use "${SIDDataPath}/fl_sedd_2010_core_short.dta", clear
 	append using "${SIDDataPath}/fl_sid_2010_core_short.dta"

	forval i = 2011/2016 {
		append using "${SIDDataPath}/fl_sedd_`i'_core_short.dta"
		append using "${SIDDataPath}/fl_sid_`i'_core_short.dta"
	}

	keep 	if hcup_ed > 0
	gen 	post = 0
	replace post = 1 if year > 2013

	gen 	medicare = 0
	replace medicare = 1 if pay1 == 1

	gen 	trad_medicare = 0
	replace trad_medicare = 1 if pay1_x == "A"

	gen 	nonmedicare18 = 0
	replace nonmedicare18 = 1 if pay1!=1 & age >= 18

	gen 	edhour_2 = edhour
	replace edhour_2 = (2400 - edhour)*(-1) if edhour > 1200

	// Create variables from last visit
	sort visitlink daystoevent
	// Last visit
		by visitlink: gen 	dayssincelastvisit = daystoevent[_n] - daystoevent[_n-1]
			label variable  dayssincelastvisit 	"Days since last visit"
		by visitlink: gen 	los_lsvi    = los[_n-1]
			label variable  los_lsvi 			"LOS last visit"
		by visitlink: gen 	tchg_lsvi   = totchg[_n-1]
			label variable  tchg_lsvi  			"Total charges last visit "
		by visitlink: gen 	IP_lsvi     = inpatient[_n-1] /* dummy for whether next visit is inpatient or not */
			label variable  IP_lsvi 			"Last visit was inpatient"
		by visitlink: gen 	dx1_lsvi    = dx1[_n-1]
			label variable  dx1_lsvi 			"Dx1 last visit"
		by visitlink: gen 	orprc_lsvi  = orproc[_n-1]
			label variable  orprc_lsvi 			"OR Proc. last visit"
		by visitlink: gen 	smdx1_lsvi  = cond(dx1 == dx1_lsvi, 1, 0)
			label variable  smdx1_lsvi 			"Last visit was same dx1"

		by visitlink: gen 	ccs1_lsvi   = dxccs1[_n-1]
			label variable  ccs1_lsvi 			"DxCCS1 last visit"
		by visitlink: gen   smccs1_lsvi = cond(dxccs1 == ccs1_lsvi, 1, 0)
			label variable  smccs1_lsvi 		"Last visit was same dxccs1"

	// Last inpatient
		gen daystoevent_inpatient = daystoevent if inpatient == 1
		* bysort visitlink (daystoevent_inpatient): gen nth_inpatient = _n
		bysort visitlink (daystoevent): replace daystoevent_inpatient = daystoevent_inpatient[_n-1] if daystoevent_inpatient == .
		bysort visitlink (daystoevent): gen 	dayssincelastinpatient = daystoevent[_n] - daystoevent_inpatient[_n-1]
			label variable dayssincelastinpatient "Days since last inpatient"

	// Next visit
		by visitlink: gen 	daystonextvisit = daystoevent[_n+1] - daystoevent[_n]
			label variable  daystonextvisit 		"Days to next visit"
		by visitlink: gen 	los_nxtvi    = los[_n+1]
			label variable  los_nxtvi 			"LOS next visit"
		by visitlink: gen 	tchg_nxtvi   = totchg[_n+1]
			label variable  tchg_nxtvi 			"Total charges next visit"
		by visitlink: gen 	IP_nxtvi     = inpatient[_n+1] /* dummy for whether next visit is inpatient or not */
			label variable  IP_nxtvi 			"Next visit is inpatient"
		by visitlink: gen 	dx1_nxtvi    = dx1[_n+1]
			label variable  dx1_nxtvi 			"Dx1 next visit"
		by visitlink: gen 	orprc_nxtvi  = orproc[_n+1]
			label variable  orprc_nxtvi 			"OR Proc. next visit"
		by visitlink: gen 	nxtvi_30d    = cond(daystonextvisit <= 30, 1, 0)
			label variable  nxtvi_30 			"Next visit in 30 days"
		by visitlink: gen 	nxtvi_60d    = cond(daystonextvisit <= 60, 1, 0)
			label variable  nxtvi_60 			"Next visit in 60 days"
		by visitlink: gen 	nxtvi_90d    = cond(daystonextvisit <= 90, 1, 0)
			label variable  nxtvi_90 			"Next visit in 90 days"
		by visitlink: gen 	smdx1_nxtvi  = cond(dx1 == dx1_nxtvi, 1, 0)
			label variable  smdx1_nxtvi 		"Next visit is same dx1"

		by visitlink: gen 	ccs1_nxtvi   = dxccs1[_n+1]
			label variable  ccs1_nxtvi 			"DxCCS1 next visit"
		by visitlink: gen   smccs1_nxtvi = cond(dxccs1 == ccs1_nxtvi, 1, 0)
			label variable  smccs1_nxtvi 		"Next visit is same dxccs1"

	// Count days since last visit/last inpatient
		gen sincevi_30 = cond(dayssincelastvisit <= 30, 1, 0)
		gen sincevi_60 = cond(dayssincelastvisit <= 60, 1, 0)
		gen sincevi_90 = cond(dayssincelastvisit <= 90, 1, 0)

		gen sinceIP_30 = cond(dayssincelastinpatient <= 30, 1, 0)
		gen sinceIP_60 = cond(dayssincelastinpatient <= 60, 1, 0)
		gen sinceIP_90 = cond(dayssincelastinpatient <= 90, 1, 0)

	// add in charges
	merge 1:1 key using "${SIDDataPath}/charges/fl_sid_chgs.dta"
	drop if _merge == 2
	drop _merge

	merge 1:1 key using "${SIDDataPath}/charges/fl_sedd_chgs.dta", update replace
	drop if _merge == 2
	drop _merge
	save "${SIDDataPath}/sidsedd_merged_20102016.dta", replace


*-------------------------------------------------- 
* For each year, create SID + SEDD merged dataset for machine learning algorithm
*--------------------------------------------------
// List of variables in datasets for each year
	local chgs_varlist       chg* key

	local core_varlist_10_15 age ahour aweekend daystoevent dhour died disp_x dispub04 dispuniform dqtr dshospid /// 
							 dx* e_ccs* ecode* female hcup_ed hcup_os hispanic_x homeless hospbrth hospst key los los_x mdnum* medincstq nchronic ndx necode ///
							 neomat npr orproc pay1 pay1_x pl_cbsa pl_ur_cat4 pointoforigin_x pointoforiginub04 pr* pstate pstco2 race race_x totchg totchg_x visitlink year ///
							 zip zipinc_qrtl 

	local core_varlist_15q4 age ahour aweekend daystoevent dhour died disp_x dispub04 dispuniform dqtr dshospid /// 
							dx* female hcup_ed hcup_os hispanic_x homeless hospbrth hospst key los los_x mdnum* medincstq nchronic ndx ///
							neomat npr orproc pay1 pay1_x pl_cbsa pl_ur_cat4 pointoforigin_x pointoforiginub04 pr* pstate pstco2 race race_x totchg totchg_x visitlink year ///
							zip zipinc_qrtl 

	local core_varlist_16   age ahour aweekend daystoevent dhour died disp_x dispub04 dispuniform dqtr dshospid /// 
						   	dx* female hcup_ed hcup_os hispanic_x homeless hospst key los los_x mdnum* medincstq ndx ///
							npr pay1 pay1_x pl_cbsa pl_ur_cat4 pointoforigin_x pointoforiginub04 pr* pstate pstco2 race race_x totchg totchg_x visitlink year ///
							zip zipinc_qrtl 

	local dx_varlist_10_15  chron* dx* e_mccs* key multinjury pclass* prmccs* 

	local dx_varlist_15q4   chron* dx* key multinjury pclass* prmccs* 

// List of variables missing in datasets for each year
	local core_missing_15q4  = "e_ccs1 e_ccs2 e_ccs3 ecode1 ecode2 ecode3 necode"
	local core_missing_16    = "e_ccs1 e_ccs2 e_ccs3 ecode1 ecode2 ecode3 necode dxccs1 dxccs2 dxccs3 dxccs4 dxccs5 dxccs6 dxccs7 dxccs8 dxccs9 dxccs10 neomat orproc prccs1 prccs2 prccs3 prccs4 prccs5 hospbrth nchronic"
	local dx_missing_15q4    = "e_mccs1 e_mccs2 e_mccs3"
	local dx_missing_16      = "chron1 chron2 chron3 chron4 chron5 chron6 chron7 chron8 chron9 chron10 dxmccs1 dxmccs2 dxmccs3 dxmccs4 dxmccs5 dxmccs6 dxmccs7 dxmccs8 dxmccs9 dxmccs10 e_mccs1 e_mccs2 e_mccs3 multinjury pclass1 pclass2 pclass3 pclass4 pclass5 prmccs1 prmccs2 prmccs3 prmccs4 prmccs5"

 forval i = 2010/2016{ 
	 	
		if `i' < 2015{

			// Merge all inpatient datasets for a year 
				use "${SIDPath}/`i'/fl_sid_`i'_chgs.dta", clear
				rename *, lower 
				keep 	   `chgs_varlist'
				duplicates drop key, force
				merge 	   1:1 key using "${SIDPath}/`i'/fl_sid_`i'_core.dta"
				rename *, lower 
				keep 	   `chgs_varlist' `core_varlist_10_15' edhour 
				duplicates drop key, force
				merge 	   1:1 key using "${SIDPath}/`i'/fl_sid_`i'_dx_pr_grps.dta" 
				rename *, lower 
				if year == 2010{
					gen multinjury = ""
				}
				keep 			`chgs_varlist' `core_varlist_10_15' `dx_varlist_10_15' edhour 
				gen inpatient = 1
				save "${SIDDataPath}/fl_sid_`i'_all_short.dta", replace
				
			// Merge all outpatient datasets for a year
				use "${SEDDPath}/`i'/fl_sedd_`i'_chgs.dta", clear
				rename *, lower 
				keep 		`chgs_varlist'
				duplicates drop key, force
				merge 		1:1 key using "${SEDDPath}/`i'/fl_sedd_`i'_core.dta"
				rename *, lower 
				keep 		`chgs_varlist' `core_varlist_10_15'
				duplicates drop key, force
				merge 		1:1 key using "${SEDDPath}/`i'/fl_sedd_`i'_dx_pr_grps.dta"
				rename *, lower 
				if year == 2010{
					gen multinjury = ""
				}
				keep 		`chgs_varlist' `core_varlist_10_15' `dx_varlist_10_15'
				rename ahour edhour
				gen inpatient = 0
				save "${SIDDataPath}/fl_sedd_`i'_all_short.dta", replace
				
		} 

		if `i' == 2015{

			// Merge all inpatient datasets for 2015 q1q3 
				use "${SIDPath}/`i'/fl_sid_`i'q1q3_chgs.dta", clear
				rename *, lower 
				keep 		`chgs_varlist'
				duplicates 	drop key, force
				merge 		1:1 key using "${SIDPath}/`i'/fl_sid_`i'q1q3_core.dta"
				rename *, lower 
				keep 		`chgs_varlist' `core_varlist_10_15' edhour
				duplicates drop key, force
				merge 		1:1 key using "${SIDPath}/`i'/fl_sid_`i'q1q3_dx_pr_grps.dta"
				rename *, lower 
				keep 		`chgs_varlist' `core_varlist_10_15' `dx_varlist_10_15' edhour
				gen inpatient = 1
				save "${SIDDataPath}/fl_sid_`i'q1q3_all_short.dta", replace

				
			// Merge all outpatient datasets for 2015 q1q3
				use "${SEDDPath}/`i'/fl_sedd_`i'q1q3_chgs.dta", clear
				rename *, lower 
				keep 	   `chgs_varlist'
				duplicates drop key, force
				merge 	   1:1 key using "${SEDDPath}/`i'/fl_sedd_`i'q1q3_core.dta"
				rename *, lower 
				keep 	   `chgs_varlist' `core_varlist_10_15'
				duplicates drop key, force
				merge 	   1:1 key using "${SEDDPath}/`i'/fl_sedd_`i'q1q3_dx_pr_grps.dta"
				rename *, lower 
				keep 	   `chgs_varlist' `core_varlist_10_15' `dx_varlist_10_15'
				gen inpatient = 0
				save "${SIDDataPath}/fl_sedd_`i'q1q3_all_short.dta", replace 
				

			// Merge all inpatient datasets for 2015 q4
				use "${SIDPath}/`i'/fl_sid_`i'q4_chgs.dta", clear
				rename *, lower
				keep 		`chgs_varlist'
				duplicates drop key, force
				merge 		1:1 key using "${SIDPath}/`i'/fl_sid_`i'q4_core.dta"
				rename *, lower
				rename 		i10_* *
				keep `chgs_varlist' `core_varlist_15q4' edhour 
				duplicates drop key, force
				merge 		1:1 key using "${SIDPath}/`i'/fl_sid_`i'q4_dx_pr_grps.dta"
				rename *, lower
				rename 		i10_* *
				keep 		`chgs_varlist' `core_varlist_15q4' `dx_varlist_15q4' edhour
				foreach var of local core_missing_15q4 { /* NOTE: if you want to define new variables based on a local list of strings, you need to define that you are using a *local* (not a varlist, otherwise it looks for those vars) and do not use the `' around the name*/
					gen `var' = ""
				}

				foreach var of local dx_missing_15q4 {
					gen `var' = ""
				}
				gen inpatient = 1
				save "${SIDDataPath}/fl_sid_`i'q4_all_short.dta", replace
				
			// Merge all outpatient datasets for 2015 q4
				use "${SEDDPath}/`i'/fl_sedd_`i'q4_chgs.dta", clear
				rename *, lower
				keep	   `chgs_varlist'
				duplicates drop key, force
				merge 	   1:1 key using "${SEDDPath}/`i'/fl_sedd_`i'q4_core.dta"
				rename *, lower
				rename 	   i10_* *
				keep 	   `chgs_varlist' `core_varlist_15q4'
				duplicates drop key, force
				merge 		1:1 key using "${SEDDPath}/`i'/fl_sedd_`i'q4_dx_pr_grps.dta"
				rename *, lower
				rename 		i10_* *
				keep 		`chgs_varlist' `core_varlist_15q4' `dx_varlist_15q4'
				foreach var of local core_missing_15q4 {
					gen `var' = ""
				}

				foreach var of local dx_missing_15q4 {
					gen `var' = ""
				} 
				rename ahour edhour
				gen inpatient = 0
			save "${SIDDataPath}/fl_sedd_`i'q4_all_short.dta", replace
			
			
			use "${SIDDataPath}/fl_sid_`i'q1q3_all_short.dta", clear
			append using "${SIDDataPath}/fl_sid_`i'q4_all_short.dta", force
			save "${SIDDataPath}/fl_sid_`i'_all_short.dta", replace
			
		} 

		if `i' == 2016 {
			// Merge all inpatient datasets for 2016
				use "${SIDPath}/`i'/fl_sid_`i'_chgs.dta", clear
				rename *, lower
				keep 	   `chgs_varlist'
				duplicates drop key, force
				merge 	   1:1 key using "${SIDPath}/`i'/fl_sid_`i'_core.dta"
				rename *, lower
				rename	   i10_* *
				keep 	   `chgs_varlist' `core_varlist_16' edhour
				foreach var of local core_missing_16 {
					gen `var' = ""
				}
				foreach var of local dx_missing_16 {
					gen `var' = ""
				}
				gen inpatient = 1
				save "${SIDDataPath}/fl_sid_`i'_all_short.dta", replace
				
			// Merge all outpatient datasets for 2016
				use "${SEDDPath}/`i'/fl_sedd_`i'_chgs.dta", clear
				rename *, lower
				keep 	   `chgs_varlist'
				duplicates drop key, force
				merge 	   1:1 key using "${SEDDPath}/`i'/fl_sedd_`i'_core.dta"
				rename *, lower
				rename 	   i10_* *
				keep 	   `chgs_varlist' `core_varlist_16'
				foreach var of local core_missing_16 {
					gen `var' = ""
				}

				foreach var of local dx_missing_16 {
					gen `var' = ""
				}
				rename ahour edhour
				gen inpatient = 0
				save "${SIDDataPath}/fl_sedd_`i'_all_short.dta", replace
				
		} 
}

log close


